package com.admin.interfaces.web;

import com.admin.application.PlayService;
import com.admin.domain.modle.Play;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * description
 * User: WeiDa
 * Date: 2017-4-11
 * Time: 16:41
 */
@Controller
@RequestMapping("/play")
public class PlayController {

    @Autowired
    protected PlayService playService;

    @RequestMapping(method = RequestMethod.POST, value = "/add")
    public String create(Play play) {
        playService.create(play);
        return "redirect:/play";
    }

    @RequestMapping(value = "/{playId}/modify", method = RequestMethod.POST)
    public String modify(@PathVariable("playId") String playId, Play play) {
        play.setPlayId(playId);
        playService.modify(play);
        return "redirect:/play/list";
    }


    @RequestMapping(value = "/{id}/delete", method = RequestMethod.DELETE)
    @ResponseBody
    public void delete(@PathVariable("id") String id) {
        playService.delete(id);
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String toform(@RequestParam(name = "playId", required = false) String playId, Model model) {
        String api = "/play/add";
        if (StringUtils.isNotBlank(playId)) {
            model.addAttribute("play", playService.get(playId));
            api = "/play/" + playId + "/modify";
        }
        model.addAttribute("api", api);
        return "play/form";
    }

    /**
     * description 首次初始化
     * User: WeiDa
     * Date: 2017-6-6
     * Time: 21:01
     */
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model, @RequestParam(value = "key", required = false) String key) {
        model.addAttribute("key", key);
        model.addAttribute("list", playService.list(key));
        return "play/list";
    }

    /**
     * description 再次编辑
     * User: WeiDa
     * Date: 2017-6-6
     * Time: 21:01
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list2(Model model, @RequestParam(value = "key", required = false) String key) {
        model.addAttribute("key", key);
        model.addAttribute("list", playService.list2(key));
        return "play/list2";
    }


}
